package com.example.select_lesson;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.balysv.materialmenu.MaterialMenuDrawable;


public class HomeActivity extends ActionBarActivity implements View.OnClickListener {

    private MaterialMenuDrawable materialMenu;
    private MyDataBaseHelper dbHelper;
    private SQLiteDatabase db;
    private String studentId;
    private String studentName;
    private TextView studentNameTextView;
    private SharedPreferences sp;
    private TextView studentClassTextView;

    private TextView chooseClass;
    private TextView searchClass;
    private TextView studentInformation;
    private TextView exit;

    private int classNumber = 0;

    public static void actionStart(Context context, String studentId) {
        Intent intent = new Intent(context, HomeActivity.class);
        intent.putExtra("student_id", studentId);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.my_dark_blue));
        }
        sp = this.getSharedPreferences("userInfo", Context.MODE_APPEND);

        //获取intent的studentId
        studentId = getIntent().getStringExtra("student_id");
        //数据库通过studentId获取student信息
        dbHelper = new MyDataBaseHelper(getApplicationContext(), "StudentManager.db", null, 1);
        dbHelper.getWritableDatabase();
        db = dbHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from Student where studentId=?",
                new String[] {studentId});
        if (cursor.moveToFirst()) {
            do {
                studentName = cursor.getString(cursor.getColumnIndex("name"));
            } while (cursor.moveToNext());
        }
        cursor.close();

        cursor = db.rawQuery("select * from SELECT_LESSON where studentId=?",
                new String[] {studentId});
        if (cursor.moveToFirst()) {
            do {
                classNumber++;
            } while (cursor.moveToNext());
        }
        cursor.close();
        //初始化控件
        init();

        setToolbar();
    }

    private void init() {
        studentNameTextView = (TextView) findViewById(R.id.tv_student_name);
        studentNameTextView.setText(studentName + "同学，你好！");
        studentClassTextView = (TextView) findViewById(R.id.tv_student_class);
        if (classNumber != 0) {
            studentClassTextView.setText("已选课程：" + classNumber + "节");
        } else {
            studentClassTextView.setText("你还没有进行选课，快去！");
        }

        chooseClass = (TextView) findViewById(R.id.btn_choose_class);
        chooseClass.setOnClickListener(this);
        searchClass = (TextView) findViewById(R.id.btn_search_class);
        searchClass.setOnClickListener(this);
        studentInformation = (TextView) findViewById(R.id.btn_student_information);
        studentInformation.setOnClickListener(this);
        exit = (TextView) findViewById(R.id.btn_exit);
        exit.setOnClickListener(this);
    }

    private void setToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("  选课啦");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                // Handle your drawable state here
                switch (materialMenu.getIconState()) {
                    case BURGER:
                        materialMenu.animateIconState(MaterialMenuDrawable.IconState.ARROW, false);
                        break;
                    case ARROW:
                        materialMenu.animateIconState(MaterialMenuDrawable.IconState.BURGER, false);
                        break;
                    case X:
                        break;
                    case CHECK:
                        break;
                }

            }
        });
        materialMenu = new MaterialMenuDrawable(this, Color.WHITE, MaterialMenuDrawable.Stroke.THIN);
        materialMenu.animateIconState(MaterialMenuDrawable.IconState.BURGER, false);
        toolbar.setNavigationIcon(materialMenu);
        materialMenu.setNeverDrawTouch(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_choose_class:
                ChooseClassTypeActivity.actionStart(getApplicationContext(), studentId);
                break;
            case R.id.btn_search_class:
                SearchClassActivity.actionStart(getApplicationContext(), studentId);
                break;
            case R.id.btn_student_information:
                StudentInformationActivity.actionStart(getApplicationContext(), studentId);
                break;
            case R.id.btn_exit:
                sp.edit().putBoolean("ISCHECK", false).apply();
                sp.edit().putBoolean("AUTOISCHECK", false).apply();
                Intent intent = new Intent(this, MainActivity.class);
                this.startActivity(intent);
                finish();
                break;
        }
    }
}
